from typing import Literal
from copy import deepcopy
from dfJinja import  dfTemplate as  Template
import shelve
from config import database_path
from loguru import logger
import  pandas as pd

JOIN_TYPE=Literal["left", "right", "inner", "outer", "cross"]

class join_table:
    def __init__(self, config: dict) -> None:
        self.config=config
        type:JOIN_TYPE=config['type']
        self.left_table_name=config['left_table']
        self.right_table_name=config['right_table']
        self.output_table_name=config['output_table']
        
        left_list=self.config['args']['left']
        right_list=self.config['args']['right']
        
        with shelve.open(database_path,writeback=True) as db:
            self.left_table=db['tables_dict'].get(self.left_table_name,[])
            self.right_table=db['tables_dict'].get(self.right_table_name,[])
            self.output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            
            df=pd.merge(pd.DataFrame(self.left_table), pd.DataFrame(self.right_table), how=type,left_on=left_list,right_on=right_list,suffixes=['', '_1'])
            df.fillna('',inplace=True)
            self.output_table+=df.to_dict('records')
            db['tables_dict'][self.output_table_name]=self.output_table



        

    

        

    

        
        

        